package com.slytherin.sns.spider.base;

import com.slytherin.sns.spider.core.SlytherinScript;
import com.slytherin.sns.spider.core.SlytherinSpider;
import com.slytherin.sns.spider.proxy.Proxy;
import com.slytherin.sns.spider.proxy.SimpleProxyProvider;
import com.slytherin.sns.spider.work.Downloader;
import com.slytherin.sns.spider.work.SlytherinDownloader;
import us.codecraft.webmagic.Site;

public class SpiderBuilder {

    private SlytherinScript script;

    private SlytherinSpider spider;
    /**
     *  是否自定义downloader 测试
     */
    private Downloader downloader;

    public SpiderBuilder(SlytherinScript script) {
        this.script = script;
    }

    /**
     *  用于构建脚本
     * @Param proxyFlag 是否构建代理使用代理构建爬虫
     * @return
     */
    public SpiderBuilder build(Downloader downloader) {
        this.downloader = downloader;
        return this;
    }

    /**
     *  用于构建脚本
     * @Param proxyFlag 是否构建代理使用代理构建爬虫
     * @return
     */
    public SpiderBuilder build(String seed,Boolean proxyFlag) {
        String host = "123";
        Integer port = 80;
        SlytherinDownloader slytherinDownloader = new SlytherinDownloader();
        slytherinDownloader.setThread(3);
        SimpleProxyProvider proxyProvider = SimpleProxyProvider.from(new Proxy(host, port));
        slytherinDownloader.setProxyProvider(proxyProvider);
        this.spider = SlytherinSpider.create(script).setSite(Site.me()).thread(3).addRequest(script.seed(seed)).setDownloader(slytherinDownloader);
        return this;
    }

    /**
     *  启动脚本
     */
    public void run() {
       this.spider.run();

    }




}
